//#define TEST_API
//#define NO_SDK

using Cysharp.Threading.Tasks;
using UnityEngine;

#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
#elif WEIXINMINIGAME
using WeChatWASM;
#elif BYTEDANCEMINIGAME
#elif QGMINIGAME
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
#elif GOOGLE_PLAY
#else
#endif
#else
#endif
#else
#endif

namespace Runtime.SDK {
    public static partial class DFR_API {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
#elif WEIXINMINIGAME
        private static WXGameClubButton gameClubBtn;

#elif BYTEDANCEMINIGAME
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
#elif GOOGLE_PLAY
#else
#endif
#else
#endif
#else
#endif
        public delegate UniTaskVoid GameClubButtonOnClickCallback();
        public static async UniTask CreateGameClubButton(Transform btnTransform, GameClubButtonOnClickCallback callback) {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
            await UniTask.Yield();
#elif WEIXINMINIGAME
            //我要先找准创建按钮的位置
            UnityEngine.Debug.Log("准备创建游戏圈按钮");

            //获取按钮的尺寸和位置
            var rect = (RectTransform)btnTransform;
            Vector2 size = rect.sizeDelta;
            Vector2 pos = btnTransform.localPosition;

            //真机的屏幕尺寸
            var screenHeight = (float)WX.GetSystemInfoSync().screenHeight;
            var screenWidth = (float)WX.GetSystemInfoSync().screenWidth;

            //我还需要计算一个比例，因为真机的屏幕尺寸和编辑器内不同
            var ratioH = screenHeight / 1280f;
            var ratioW = screenWidth / 720f;

            //创建游戏圈按钮
            WXCreateGameClubButtonParam param = new()
            {
                type = GameClubButtonType.text,
                style = new GameClubButtonStyle
                {
                    //left和top是从最左上角开始计算的，最左上角为(0,0) )
                    left = (int)(pos.x + (float)WX.GetSystemInfoSync().screenWidth / 2 - (size.x * ratioW) / 2),
                    top = Mathf.Abs((int)((pos.y * ratioH) - (float)WX.GetSystemInfoSync().screenHeight / 2 + (size.y * ratioH) / 2)),

                    //我所创建的按钮的尺寸，需要相应的缩小或放大
                    width = (int)(size.x * ratioW),
                    height = (int)(size.y * ratioH),
                }
            };

            gameClubBtn = WXBase.CreateGameClubButton(param);
            gameClubBtn.Show();
            ApplicationShow += _ =>
            {
                callback?.Invoke();
            };

            await UniTask.Yield();

#elif BYTEDANCEMINIGAME
            await UniTask.Yield();
#elif QGMINIGAME
            await UniTask.Yield();
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
            await UniTask.Yield();
#elif GOOGLE_PLAY
            await UniTask.Yield();
#else
#endif
#else
            await UniTask.Yield();
#endif
#else
            // if (transform.GetComponent<Button>())
            // {
            //     transform.GetComponent<Button>().onClick.AddListener(() =>
            //     {
            //         //打开游戏圈
            //         callback?.Invoke();
            //     });
            // }

            await UniTask.Yield();
            // return true;
#endif
        }

        public static void IsJoinedGameClub(GameClubButtonOnClickCallback callback) {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
#elif WEIXINMINIGAME
            GetGameClubDataOption option = new GetGameClubDataOption();
            option.dataTypeList = new DataType[] { new DataType { type = 1 } };
            WX.GetGameClubData(new GetGameClubDataOption()
            {
                success = (res) =>
                {
                    Debug.Log("玩家已加入游戏圈");
                    UnityEngine.Debug.Log(res.encryptedData);
                    callback?.Invoke();
                    UnityEngine.Debug.Log("返回了success");
                },
                fail = (res) =>
                {
                    UnityEngine.Debug.Log(res.errMsg);
                    UnityEngine.Debug.Log("返回了fail");
                }
            });

#elif BYTEDANCEMINIGAME
#elif QGMINIGAME
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
#elif GOOGLE_PLAY
#else
#endif
#else
#endif
#else
            // if (transform.GetComponent<Button>())
            // {
            //     transform.GetComponent<Button>().onClick.AddListener(() =>
            //     {
            //         //打开游戏圈
            //         callback?.Invoke();
            //     });
            // }

            // await UniTask.Yield();
            // return true;
#endif
        }

        public static async UniTask DestroyGameClubButton() {
#if TEST_API || !UNITY_EDITOR
#if UNITY_WEBGL
#if NO_SDK
            await UniTask.Yield();
#elif WEIXINMINIGAME
            gameClubBtn.Destroy();
            await UniTask.Yield();
#elif BYTEDANCEMINIGAME
            await UniTask.Yield();
#elif QGMINIGAME
            await UniTask.Yield();
#else
#endif
#elif UNITY_ANDROID
#if NO_SDK
            await UniTask.Yield();
#elif GOOGLE_PLAY
            await UniTask.Yield();
#else
#endif
#else
            await UniTask.Yield();
#endif
#else
            await UniTask.Yield();
#endif
        }
    }
}
